package com.rx.mgt.controller;

import com.rx.mgt.domain.*;
import com.rx.mgt.po.UsersBo;
import com.rx.mgt.service.DomainService;
import com.rx.mgt.service.GroupService;
import com.rx.mgt.service.SysLogService;
import com.rx.mgt.service.UserInfoExtService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zxj on 2017/4/26.
 */
@Controller
public class GroupController {

    @Autowired
    SysLogService sysLogService;
    @Autowired
    GroupService groupService;
    @Autowired
    DomainService domainService;

    @Autowired
    UserInfoExtService userInfoExtService;
    /**
     * 初始化部门列表页面
     */
    @RequestMapping("/group_list.htmlx")
    public String getGroupPage(HttpServletRequest request ){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        String domain = request.getParameter("domain");
        UsersBo  usersBo = new UsersBo();
        List list = domainService.getDomainInfoExtQy(usersBo);
//        if(StringUtils.isNotBlank(domain)){
//            usersBo.setDomain(domain);
//        }
//        if("2".equals(user.getType())){
//            usersBo.setDomain(user.getDomain());
//        }
        request.setAttribute("domainList",list);
        request.setAttribute("dom",domain);
        return "group/group_list";
    }

    /**
     * 获取部门列表
     */
    @RequestMapping("/queryGroupByDomain.htmlx")
    @ResponseBody
    public List<Group> queryGroupByDomain(HttpServletRequest request ){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        String domain = request.getParameter("domain");
        if("2".equals(user.getType())){
            domain = user.getDomain();
        }
        List<Group> list = groupService.findGroupByDomain(domain);
        for(Group g:list){
            if(0==g.getpId()){
                g.setOpen(true);
            }
        }

        return list;
    }
    /***
     *添加新部门的页面初始化
     */
    @RequestMapping("/groupAddInitPage.htmlx")
    public String groupAddInitPage(HttpServletRequest request){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        UsersBo usersBo = new UsersBo();
        if("2".equals(user.getType())){

            usersBo.setDomain(user.getDomain());
        }
        List list = domainService.getDomainInfoExt(usersBo);
        request.setAttribute("domainList",list);
        return "group/group-add";
    }

    /**
     * 添加新部门
     */
    @RequestMapping(value = "/groupSave.htmlx",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addGroup(HttpServletRequest request,Group group){
        Map<String,Object> rs = new HashMap<String, Object>();
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        if("2".equals(user.getType())){
            group.setDomain(user.getType());
        }
        groupService.saveGroup(group);
        rs.put("id",group.getId());
        rs.put("pId",group.getpId());
        return rs;
    }
    /**
     * 修改部门名称
     */
    @RequestMapping("/updateGroup.htmlx")
    @ResponseBody
    public Map<String,Object> updateGroup(Group group){
        Map<String,Object> rs = new HashMap<String, Object>();
        groupService.updateGroup(group);
        rs.put("msg","success");
        return rs;
    }
    /***
     * 删除企业或删除部门
     * @param
     * @param
     * @param
     * @return
     */
    @RequestMapping("/deleteGroupById")
    @ResponseBody
    public Map<String,Object> deleteGroupById(@RequestParam("id")String id, @RequestParam("pId")String pId){
        Map<String,Object> rs = new HashMap<String, Object>();
        if(StringUtils.isNotEmpty(id)){
            groupService.delete(Long.valueOf(id));
        }
        rs.put("msg","success");
        return rs;
    }
    /**
     * 根据部门Id查询用户
     */
    @RequestMapping("/queryGroupMembers.htmlx")
    @ResponseBody
    public Map<String,Object> queryGroupMembers(@RequestParam("groupId")String groupId){
        Map<String,Object> rs = new HashMap<String, Object>();
        List<UserInfoExt> list = groupService.queryGroupMembersByGroupId(groupId);
        rs.put("tableData",list);
        return rs;
    }

    /**
     * 添加企业成员
     */
    @RequestMapping("/group_users_add.htmlx")
    public String addGroupUser(ModelMap model, HttpServletRequest request, UserInfoExt userInfoExt){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        UsersBo  usersBo = new UsersBo();
        if("2".equals(user.getType())){
            usersBo.setDomain(user.getDomain());
        }
        String domain  = request.getParameter("domain");
       // List list = domainService.getDomainInfoExt(usersBo);
        List<Group> grouplist = groupService.findGroupByDomain(domain);
       // model.addAttribute("domainList",list);
        model.addAttribute("allQuotaGroup",grouplist);
        Map map =new HashMap();
        map.put("result", "success");
        if(org.springframework.util.StringUtils.isEmpty(userInfoExt.getId())){
            model.addAttribute("user",userInfoExt);
            model.addAttribute("method","add");
            return "user/group_users_add";
        }
        UserInfoExt info= userInfoExtService.get(userInfoExt.getId());
        if(info!=null){
            String userName=info.getUser_name();
            info.setUser_name(userName.substring(0, userName.indexOf("@")));
            model.addAttribute("user",info);
        }
        System.out.println("111111111111111");
        return "user/group_users_add";


    }
    /**
     * 部门管理员列表初始化
     */
    @RequestMapping("/group-manage-list.htmlx")
    public String groupManagersList(HttpServletRequest request){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        UsersBo  usersBo = new UsersBo();
        if("2".equals(user.getType())){
            usersBo.setDomain(user.getDomain());
        }
        String domain = request.getParameter("domain");
        List list = domainService.getDomainInfoExt(usersBo);
        request.setAttribute("domainList",list);
        request.setAttribute("dom",domain);
        return "group/group-manage-list";
    }
    /**
     * 查询部门管理员列表
     */
    @RequestMapping("/queryGroupMangerList.htmlx")
    @ResponseBody
    public Map<String,Object> queryGroupMangerList(HttpServletRequest request){
        Map<String,Object> rs = new HashMap<String, Object>();
        String domain = request.getParameter("domain");
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        if("2".equals(user.getType())){
            domain = user.getDomain();
        }
        List<GroupManager> list = groupService.queryGroupMangerList(domain);
        rs.put("tableData",list);
        return  rs;
    }
    /**
     * 添加部门管理员页面初始化
     */
    @RequestMapping("/group_manage_add.htmlx")
    public String initGroupMangerPage(HttpServletRequest request){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        String domain = request.getParameter("domain");

        if("2".equals(user.getType())){
            domain = user.getDomain();
        }
        List<Group> list = groupService.findGroupByDomain(domain);
        request.setAttribute("group",list);
        return "group/group-manage-add";
    }

    /**
     * 修改部门管理员页面初始化
     */
    @RequestMapping("/group_manage_update.htmlx")
    public String initGroupMangerUpdatePage(HttpServletRequest request){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        String domain = request.getParameter("domain");
        String id = request.getParameter("id");

        if("2".equals(user.getType())){
            domain = user.getDomain();
        }
        GroupManager groupManager = groupService.findGroupManagerById(Long.valueOf(id));
        List<Group> list = groupService.findGroupByDomain(domain);
        request.setAttribute("group",list);
        request.setAttribute("groupManager",groupManager);
        return "group/group-manage-update";
    }

    /**
     * 添加部门管理员
     */
    @RequestMapping(value = "/groupManagerSave.htmlx",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addGroupManager(HttpServletRequest request,GroupManager manager){
        Map<String,Object> rs = new HashMap<String, Object>();
        manager.setEmail(manager.getEmail()+"@"+manager.getDomain());
        groupService.addGroupManger(manager);
        if(manager.getId()!=null){
            rs.put("msg","success");

        }else{
            rs.put("msg","error");
        }
        return  rs;
    }
    /**
     * 删除部门管理员
     */
    @RequestMapping("/deleteManager.htmlx")
    @ResponseBody
    public Map deleteGroupManager(@RequestParam("id")String id){
        Map rs = new HashMap();
        groupService.deleteGroupManager(id);

        return rs;
    }
    /**
     * 企业通讯录共享管理
     */
    @RequestMapping("/queryDomainShareList.htmlx")
    public String queryDomainShareList(HttpServletRequest request){
//        List<DomainShare> groupService.queryDomainShareList();
        return "group/group-domain-share";
    }

}
